package com.leoman.message.dao;

import com.leoman.common.dao.IBaseJpaRepository;
import com.leoman.message.entity.Message;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 信息dao
 * Created by 涂奕恒 on 2016/6/12.
 */
public interface MessageDao extends IBaseJpaRepository<Message> {

    @Query("select a from Message a where (a.toObject = '0' and a.sendDate <= ?2) or (a.toObject like ?1 and a.userId = ?3) or (a.type = 1 and a.userId = ?3) order by a.createDate desc")
    Page<Message> pageByUserType(String type, Long sendDate, Long userId, Pageable pageable);

    @Query("select count(a) from PostComment a where a.toUser.id = ?1 and a.isRead = 0")
    Integer findCountPostComment(Long userId);

    @Query("select count(a) from WorkComment a where a.toUserInfo.id = ?1 and a.isRead = 0")
    Integer findCountWorkComment(Long userId);

    @Query("select count(a) from UserChange a where a.postBase.userInfo.id = ?1 and a.isRead = 0")
    Integer findCountUserChange(Long userId);

    @Query("select count(a) from Message a where (a.userId = ?1 and a.isRead = 0 and a.type = 1) or (a.type = 0 and a.isRead = 0)")
    Integer findCountMessage(Long userId);

    @Query("select a from Message a where (a.userId = ?1 and a.isRead = 0 and a.type = 1) or (a.type = 0 and a.isRead = 0)")
    List<Message> findListMessage(Long userId);
}
